#include "main.h"
#include "74HC4051PW.h"
#include "adc.h"
#include "usart.h"

uint8_t Voltage_Flag_5V = False;
uint8_t Voltage_Flag_12V = False;
uint8_t Voltage_Flag_Input = False;
double check[3];
uint32_t ADC_OFFSET_74HC4051PW = 0;

uint32_t Switch_ADC(uint8_t Pin_Number)
{
    uint32_t adc_value = 0;
    if(Pin_Number == NTC)
    {
        HAL_GPIO_WritePin(ADC_SW0_GPIO_Port, ADC_SW0_Pin, GPIO_PIN_RESET);
        HAL_GPIO_WritePin(ADC_SW1_GPIO_Port, ADC_SW1_Pin, GPIO_PIN_RESET);
        HAL_GPIO_WritePin(ADC_SW2_GPIO_Port, ADC_SW2_Pin, GPIO_PIN_RESET);
    }
    else if(Pin_Number == MAINB_TEMP)
    {
        HAL_GPIO_WritePin(ADC_SW0_GPIO_Port, ADC_SW0_Pin, GPIO_PIN_SET);
        HAL_GPIO_WritePin(ADC_SW1_GPIO_Port, ADC_SW1_Pin, GPIO_PIN_RESET);
        HAL_GPIO_WritePin(ADC_SW2_GPIO_Port, ADC_SW2_Pin, GPIO_PIN_RESET);
    }
    else if(Pin_Number == BLANK)
    {
        HAL_GPIO_WritePin(ADC_SW0_GPIO_Port, ADC_SW0_Pin, GPIO_PIN_RESET);
        HAL_GPIO_WritePin(ADC_SW1_GPIO_Port, ADC_SW1_Pin, GPIO_PIN_SET);
        HAL_GPIO_WritePin(ADC_SW2_GPIO_Port, ADC_SW2_Pin, GPIO_PIN_RESET);
    }
    else if(Pin_Number == ADC_12V_DEC)
    {
        HAL_GPIO_WritePin(ADC_SW0_GPIO_Port, ADC_SW0_Pin, GPIO_PIN_SET);
        HAL_GPIO_WritePin(ADC_SW1_GPIO_Port, ADC_SW1_Pin, GPIO_PIN_SET);
        HAL_GPIO_WritePin(ADC_SW2_GPIO_Port, ADC_SW2_Pin, GPIO_PIN_RESET);
    }
    else if(Pin_Number == PW_TEMP)
    {
        HAL_GPIO_WritePin(ADC_SW0_GPIO_Port, ADC_SW0_Pin, GPIO_PIN_RESET);
        HAL_GPIO_WritePin(ADC_SW1_GPIO_Port, ADC_SW1_Pin, GPIO_PIN_RESET);
        HAL_GPIO_WritePin(ADC_SW2_GPIO_Port, ADC_SW2_Pin, GPIO_PIN_SET);
    }
    else if(Pin_Number == ADC_INPUT_DEC)
    {
        HAL_GPIO_WritePin(ADC_SW0_GPIO_Port, ADC_SW0_Pin, GPIO_PIN_SET);
        HAL_GPIO_WritePin(ADC_SW1_GPIO_Port, ADC_SW1_Pin, GPIO_PIN_RESET);
        HAL_GPIO_WritePin(ADC_SW2_GPIO_Port, ADC_SW2_Pin, GPIO_PIN_SET);
    }
    else if(Pin_Number == ADC_5V_DEC)
    {
        HAL_GPIO_WritePin(ADC_SW0_GPIO_Port, ADC_SW0_Pin, GPIO_PIN_RESET);
        HAL_GPIO_WritePin(ADC_SW1_GPIO_Port, ADC_SW1_Pin, GPIO_PIN_SET);
        HAL_GPIO_WritePin(ADC_SW2_GPIO_Port, ADC_SW2_Pin, GPIO_PIN_SET);
    }
    else if(Pin_Number == CURRENT_INPUT)
    {
        HAL_GPIO_WritePin(ADC_SW0_GPIO_Port, ADC_SW0_Pin, GPIO_PIN_SET);
        HAL_GPIO_WritePin(ADC_SW1_GPIO_Port, ADC_SW1_Pin, GPIO_PIN_SET);
        HAL_GPIO_WritePin(ADC_SW2_GPIO_Port, ADC_SW2_Pin, GPIO_PIN_SET);
    }
    HAL_ADCEx_Calibration_Start(&hadc1,ADC_CALIB_OFFSET,ADC_SINGLE_ENDED);
    HAL_ADC_Start(&hadc1);
    if(HAL_ADC_PollForConversion(&hadc1,0xffff) == HAL_OK)
    {
        adc_value = HAL_ADC_GetValue(&hadc1);
    }
    HAL_ADC_Stop(&hadc1);
    return adc_value;
}

double Real_Sampling_Value(uint8_t Pin_Number)
{
    double Real_Value = 0;
    uint32_t Adc_Value = Switch_ADC(Pin_Number);
    if(Pin_Number == NTC)
    {
        Real_Value = (double)Adc_Value/65535*3.3;
    }
    else if(Pin_Number == MAINB_TEMP)
    {
        Real_Value = (double)Adc_Value/65535*3.3;
    }
    else if(Pin_Number == BLANK)
    {
        Real_Value = (double)Adc_Value/65535*3.3;
    }
    else if(Pin_Number == ADC_12V_DEC)
    {
        Real_Value = (double)Adc_Value/65535*3.3*11;
    }
    else if(Pin_Number == PW_TEMP)
    {
        Real_Value = (double)Adc_Value/65535*3.3;
    }
    else if(Pin_Number == ADC_INPUT_DEC)
    {
        Real_Value = (double)Adc_Value/65535*62.3;
    }
    else if(Pin_Number == ADC_5V_DEC)
    {
        Real_Value = (double)Adc_Value/65535*3.3*14.7/4.7;
    }
    else if(Pin_Number == CURRENT_INPUT)
    {
        Real_Value = (double)Adc_Value/65535*3.3;
    }
    else
    {
        return False;
    }
    return Real_Value;
}

uint8_t Voltage_Check()
{
    if(Real_Sampling_Value(ADC_5V_DEC) <= 4.6 || Real_Sampling_Value(ADC_5V_DEC) >= 5.3)
    {
        Voltage_Flag_5V = False;
    }
    else
    {
        Voltage_Flag_5V = True;
    }

    if(Real_Sampling_Value(ADC_12V_DEC) <= 11 || Real_Sampling_Value(ADC_5V_DEC) >= 15)
    {
        Voltage_Flag_12V = False;
    }
    else
    {
        Voltage_Flag_12V = True;
    }
    if(Real_Sampling_Value(ADC_INPUT_DEC) <= 36 || Real_Sampling_Value(ADC_INPUT_DEC) >= 62  )
    {
        Voltage_Flag_Input = False;
    }
    else
    {
        Voltage_Flag_Input = True;
    }
    if(Voltage_Flag_5V * Voltage_Flag_12V * Voltage_Flag_Input == 0)
    {
        HAL_GPIO_TogglePin(LED_4_GPIO_Port, LED_4_Pin);
        return False;
    }
    HAL_GPIO_WritePin(LED_4_GPIO_Port, LED_4_Pin, GPIO_PIN_SET);
    return True;
}

